package com.mzkj.wanjulaile.mobile.api.modules.address.service;

import com.mzkj.common.utils.StringUtils;
import com.mzkj.wanjulaile.mobile.api.modules.address.entity.Address;
import com.mzkj.wanjulaile.mobile.api.modules.address.mapper.AddressMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 地址管理Service
 * @author ianly
 * @version 2016-10-12
 */
@Service("addressService")
@Transactional(readOnly = true)
public class AddressServiceImpl implements AddressService {
	@Autowired
	private AddressMapper addressMapper;

	public Address get(String id) {
		return addressMapper.get(id);
	}
	
	public List<Address> findList(Address address) {
		return addressMapper.findList(address);
	}

	@Transactional(readOnly = false)
	public void delete(Address address) {
		addressMapper.delete(address);
	}

	@Override
	@Transactional(readOnly = false)
	public void save(Address entity) {
			if (StringUtils.isBlank(entity.getId())){
				entity.setIsNewRecord(true);
				entity.preInsert();
				addressMapper.insert(entity);
			}else{
				entity.preUpdate();
				addressMapper.update(entity);
			}

	}

	/**
	 * 根据账号id获取addresses
	 * @param accountId
	 * @return
     */
	public List<Address> getListByAccountId(String accountId) {
		return addressMapper.getListByAccountId(accountId);
	}

	/**
	 * 获取默认地址
	 * @param accountId
	 * @return
     */
	public Address getDefaultAddress(String accountId) {
		return addressMapper.getDefaultAddress(accountId);
	}

	/**
	 * 设置默认地址
	 * @param map
     */
	@Transactional(readOnly = false)
	public void updateDefault(Map<String, Object> map) {
		addressMapper.updateDefault(map);
	}

	/**
	 * 取消默认地址
	 * @param accountId
	 */
	@Transactional
	public void setUnDefault(String accountId) {
		addressMapper.setUnDefault(accountId);
	}
}